import { globalTool, globalLayer } from '../BQGIS-globalVar'

// 控高分析
export function initLimitHeight({viewer = window.viewer, height = 0, bottomHeight = 0}) {
    if (!globalLayer.limitHeightLayer) {
        globalLayer.limitHeightLayer = new mars3d.layer.GraphicLayer()
        viewer.addLayer(globalLayer.limitHeightLayer)
    }
    if (!globalTool.limitHeight) {
        globalTool.limitHeight = new mars3d.thing.LimitHeight({
            color: "rgba(255,0,0,0.5)",
            height, // 限高
            bottomHeight, // 模型地面的海拔高度（单位：米）
            positions: []
        })
        viewer.addThing(globalTool.limitHeight)
    }
}

// 绘制矩形
export function drawExtent() {
    globalLayer.limitHeightLayer.startDraw({
        type: "rectangle",
        style: {
            color: "#ffff00",
            opacity: 0.3,
            clampToGround: true
        },
        success: function (graphic) {
            // 绘制成功后回调
            const positions = graphic.getOutlinePositions(false)
            globalTool.limitHeight.positions = positions

            globalLayer.limitHeightLayer.clear()
        }
    })
}

// 绘制面
export function drawPolygon() {
    globalLayer.limitHeightLayer.startDraw({
        type: "polygon",
        style: {
            color: "#ffff00",
            opacity: 0.3,
            clampToGround: true
        },
        success: function (graphic) {
            // 绘制成功后回调
            const positions = graphic.positionsShow
            globalTool.limitHeight.positions = positions

            globalLayer.limitHeightLayer.clear()
            console.log("绘制坐标为", JSON.stringify(mars3d.LngLatArray.toArray(positions))) // 方便测试拷贝坐标
        }
    })
}

export function onChangeHeight(value) {
    globalTool.limitHeight.height = value
  }

export function clear () {
    globalTool.limitHeight.clear()
}